package com.meng.message.controller.auth;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meng.message.domain.vo.ResponseVO;
import com.meng.message.domain.vo.RoleSaveVO;
import com.meng.message.domain.vo.SysRoleVO;
import com.meng.message.enums.ErrorEnum;
import com.meng.message.service.auth.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色表 前端控制器
 * @author: Xu Meng
 * @program: minions-cloud
 * @create: 2021-01-14 23:06
 **/
@Api(tags = "系统角色表")
@Slf4j
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("新增or修改 角色")
    @RequestMapping(value = "/saveRole", method = RequestMethod.POST)
    public ResponseVO<?> saveRole(@RequestBody RoleSaveVO role) throws Exception{
        boolean flag = sysRoleService.saveOrUpdateRole(role);
        return ResponseVO.result(ErrorEnum.SUCCESS,flag);
    }

    @ApiOperation("获取角色列表")
    @RequestMapping(value = "/saveRole", method = RequestMethod.GET)
    public ResponseVO<?> findRoleList(){
        List<SysRoleVO> list = sysRoleService.list(null);
        return ResponseVO.result(ErrorEnum.SUCCESS,list);
    }

    @ApiOperation("角色删除")
    @RequestMapping(value = "/delRole", method = RequestMethod.GET)
    public ResponseVO<?> delRole(@RequestParam Long id){
        UpdateWrapper<SysRoleVO> wapper = new UpdateWrapper<>();
        wapper.eq("role_id",id);
        wapper.set("del_flag","1");
        boolean flag = sysRoleService.update(wapper);
        return ResponseVO.result(ErrorEnum.SUCCESS,flag);
    }

    @ApiOperation("角色列表带分页，use")
    @RequestMapping(value = "/listPageRoles", method = RequestMethod.GET)
    public ResponseVO<?> listPageRoles(@RequestParam(required = false, defaultValue = "1", value = "pageNum") int pageNum,
                                       @RequestParam(required = false, defaultValue = "10", value = "pageSize") int pageSize,
                                       @RequestParam(required = false,  value = "search") String search) {
        Page<SysRoleVO> resultList = sysRoleService.findRoleListPage(pageNum,pageSize,search);
        return ResponseVO.result(ErrorEnum.SUCCESS,resultList);
    }

}

